// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: fengyang

#pragma once

#include <memory>
#include <string>

#include "coin2/base/string_util.h"
#include "coin2/exchange/base/symbology/currency.h"
#include "coin2/exchange/base/symbology/util.h"

namespace coin2::exchange::crypto::symbology {

class CryptoCurrency : public base::symbology::Currency,
                       public base::symbology::CurrencyFromStrMixin<CryptoCurrency> {
 public:
  using ExchangeType = base::market::ExchangeType;
  using MarketType = base::market::MarketType;
  using Currency = base::symbology::Currency;
  using base::symbology::Currency::native_currency;

 public:
  explicit CryptoCurrency(const std::string& currency)
      : Currency(currency, ToNativeCurrency(currency).value(), ExchangeType::Crypto) {}

  static std::unique_ptr<CryptoCurrency> FromNativeStrImpl(
      const std::string& native_currency_str,
      int64_t resolve_timestamp) {
    auto currency = FromNativeCurrency(native_currency_str, resolve_timestamp);
    if (currency) {
      return std::make_unique<CryptoCurrency>(currency.value());
    }
    return {};
  }

  static std::unique_ptr<CryptoCurrency> FromNormStrImpl(
      const std::string& currency_str,
      int64_t resolve_timestamp) {
    auto native_currency = ToNativeCurrency(currency_str);
    if (!native_currency) {
      return {};
    }

    auto norm_currency = FromNativeCurrency(*native_currency, resolve_timestamp);
    if (!norm_currency) {
      return {};
    }
    return std::make_unique<CryptoCurrency>(norm_currency.value());
  }

 private:
  static std::optional<std::string> ToNativeCurrency(const std::string& currency_str) {
    std::string native_currency = StringToUpperCopy(currency_str);
    if (currency_str == "BCHN") {
      native_currency = "BCH";
    } else if (currency_str == "NANO") {
      native_currency = "XNO";
    }
    return native_currency;
  }

  static std::optional<std::string> FromNativeCurrency(
      const std::string& native_currency_str,
      int64_t resolve_timestamp) {
    std::string currency_str = StringToUpperCopy(native_currency_str);
    if (native_currency_str == "BCH") {
      currency_str = "BCHN";
    } else if (native_currency_str == "XNO") {
      currency_str = "NANO";
    }

    return currency_str;
  }
};

}  // namespace coin2::exchange::crypto::symbology
